package com.qf.j2105.controller;

import com.qf.j2105.vo.ActionResult;
import lombok.extern.slf4j.Slf4j;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.authc.AuthenticationException;
import org.apache.shiro.authc.UsernamePasswordToken;
import org.apache.shiro.subject.PrincipalCollection;
import org.apache.shiro.subject.Subject;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

@RestController
@Slf4j
public class LoginController {

    @RequestMapping(value = "/login", method = RequestMethod.POST)
    public ActionResult login(@RequestParam("utel") String utel, @RequestParam("upass") String upass) {
        log.info(utel + "==" + upass);

        ActionResult actionResult = new ActionResult();
        //工厂对象
        Subject subject = SecurityUtils.getSubject();


        UsernamePasswordToken token = new UsernamePasswordToken(utel, upass);

         try {
             subject.login(token);
             if (subject.isAuthenticated()) {   //登录成功
                 Object principal = subject.getPrincipal();
                 System.out.println("principal = " + principal);
                 PrincipalCollection previousPrincipals = subject.getPreviousPrincipals();
                 System.out.println("previousPrincipals = " + previousPrincipals);
             }
         }catch (AuthenticationException a){
             return new ActionResult(-200,"登录失败",null);
         }

        return new ActionResult(-200,"登录失败",null);
    }
}
